package org.example.task.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.task.model.dto.PageDTO;
import org.example.task.model.po.RolePermission;
import org.example.task.mapper.RolePermissionMapper;
import org.example.task.model.query.RolePermissionPageQuery;
import org.example.task.model.vo.rolepermission.RolePermissionVO;
import org.example.task.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-06-30
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Override
    public PageDTO<RolePermissionVO> getRolePermissionList(RolePermissionPageQuery rolePermissionPageQuery) {
        Page<RolePermission> rolePermissionPage = rolePermissionPageQuery.toPage();
        lambdaQuery().eq(rolePermissionPageQuery.getRoleId() != null, RolePermission::getRoleId, rolePermissionPageQuery.getRoleId())
                .eq(rolePermissionPageQuery.getPermissionId() != null, RolePermission::getPermissionId, rolePermissionPageQuery.getPermissionId())
                .page(rolePermissionPage);
        return PageDTO.of(rolePermissionPage, RolePermissionVO.class);
    }

    @Override
    public boolean removeRolePermissionById(Long roleId, Long permissionId) {
        return lambdaUpdate().eq(RolePermission::getRoleId, roleId).eq(RolePermission::getPermissionId, permissionId).remove();
    }
}
